// pages/posts/post-detail/post-detail.js
var postsData = require('../../../data/posts-data.js')
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isPlayingMusic: false
    },

    onCollectionTap: function(event) {
        var postsCollected = wx.getStorageSync('post_collected')
        var postCollected = postsCollected
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //获取全局变量
        var globalData = app.globalData;
        // g_isPlayingMusic

        //接受传过来的参数id
        var postId = options.id;
        this.data.currentPostId = postId;
        var postData = postsData.postList[postId];
        this.setData({
            postData: postData
        })

        // var postsCollected = {
        //     1: 'true',
        //     2: 'false',
        //     3: 'true'
        //     ...
        // }

        var postsCollected = wx.getStorageSync('posts_Collected')
        if (postsCollected) {
            var postCollected = postsCollected[postId]
            this.setData({
                collected: postCollected
            })
        } else {
            var postsCollected = {}
            postsCollected[postId] = false
            wx.setStorageSync('posts_Collected', postsCollected)
        }

        if (globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === this.data.currentPostId){
            this.setData({
                isPlayingMusic: true
            })
        }

        this.setMusicMonitor();

    },

    setMusicMonitor: function(){
        var that = this;
        wx.onBackgroundAudioPlay(function () {
            that.setData({
                isPlayingMusic: true
            })
            app.globalData.g_isPlayingMusic = true;
            app.globalData.g_currentMusicPostId = that.data.currentPostId;
        })
        wx.onBackgroundAudioPause(function () {
            that.setData({
                isPlayingMusic: false
            })
            app.globalData.g_isPlayingMusic = false;
            app.globalData.g_currentMusicPostId = null;
        })
        wx.onBackgroundAudioStop(function () {
            that.setData({
                isPlayingMusic: false
            })
            app.globalData.g_isPlayingMusic = false;
            app.globalData.g_currentMusicPostId = null;
        })
    },

    onCollectionTap: function(event) {
        this.getPostsCollectedSys(); //取缓存同步方法
        // this.getPostsCollectedAsy(); //取缓存异步方法
    },

    //取缓存异步方法
    getPostsCollectedAsy: function() {
        var that = this;
        wx.getStorage({
            key: 'posts_Collected',
            success: function(res) {
                var postsCollected = res.data;
                var postCollected = postsCollected[that.data.currentPostId];
                //收藏变成未收藏, 未收藏变成收藏
                postCollected = !postCollected;
                postsCollected[that.data.currentPostId] = postCollected;
                that.showToast(postsCollected, postCollected);
            }
        })
    },

    //取缓存同步方法
    getPostsCollectedSys: function() {
        var postsCollected = wx.getStorageSync('posts_Collected');
        var postCollected = postsCollected[this.data.currentPostId];
        //收藏变成未收藏, 未收藏变成收藏
        postCollected = !postCollected;
        postsCollected[this.data.currentPostId] = postCollected;
        this.showToast(postsCollected, postCollected);
    },

    showModel: function(postsCollected, postCollected) {
        var that = this;
        wx.showModal({
            title: '收藏',
            content: postCollected ? '收藏该文章?' : '取消收藏该文章?',
            showCancel: 'true',
            cancelText: '取消',
            cancelColor: '#333',
            confirmText: '确认',
            confirmColor: '#405f80',
            success(res) {
                if (res.confirm) {
                    //更新文章是否的缓存值
                    wx.setStorageSync('posts_Collected', postsCollected);
                    //更新数据绑定变量,从而实现切换图片
                    that.setData({
                        collected: postCollected
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    showToast: function(postsCollected, postCollected) {
        //更新文章是否的缓存值
        wx.setStorageSync('posts_Collected', postsCollected);
        //更新数据绑定变量,从而实现切换图片
        this.setData({
            collected: postCollected
        });
        wx.showToast({
            title: postCollected ? '收藏成功' : '取消成功',
            icon: 'success',
            duration: 1000
        })
    },

    onShareTap: function(event) {
        var itemList = ['分享给微信好友', '分享给朋友圈', '分享到QQ', '分享到微博'];
        wx.showActionSheet({
            itemList: itemList,
            itemColor: '#405f80',
            success(res) {
                console.log(res.tapIndex)
                wx.showModal({
                    title: '用户' + itemList[res.tapIndex],
                    content: '用户是否取消?' + res.cancel + '现在无法实现分享功能'
                })
            },
            fail(res) {
                console.log(res.errMsg)
            }
        })

    },

    onMusicTap: function(event) {
     
        var currentPostId = this.data.currentPostId;
        var isPlayingMusic = this.data.isPlayingMusic;
        if (isPlayingMusic){
            // 暂停
            wx.pauseBackgroundAudio();
            this.setData({
                isPlayingMusic: false
            })
        }else{
            wx.playBackgroundAudio({
                dataUrl: postsData.postList[currentPostId].music.url,
                title: postsData.postList[currentPostId].music.title,
                coverImgUrl: postsData.postList[currentPostId].music.coverImg
            });
            this.setData({
                isPlayingMusic: true
            })
        }
        

        

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})